package com.hiv.config;

import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.security.PrivateKey;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhanghong
 * @version ConfigurationConfig.java, v 0.1 2022/1/7 15:05 下午 ConfigurationConfig Exp $$
 */
@Component
public class ConfigurationConfig {

    /**
     * 应用名称
     */
    public static String applicationName;
    /**
     * 应用名称
     */
    public static String applicationHost;
    /**
     * 文件上传前缀路径
     */
    public static String applicationUploadPath;
    /**
     * 文件下载前缀路径
     */
    public static String applicationDownloadPath;
    /**
     * 文件下载前缀路径
     */
    public static String applicationPdfPath;

    @Value("${spring.application.name}")
    public void setApplicationName(String applicationName) {
        ConfigurationConfig.applicationName = applicationName;
    }

    @Value("${application.host}")
    public void setApplicationHost(String applicationHost) {
        ConfigurationConfig.applicationHost = applicationHost;
    }

    @Value("${application.upload.path}")
    public void setApplicationUploadPath(String applicationUploadPath) {
        ConfigurationConfig.applicationUploadPath = applicationUploadPath;
    }

    @Value("${application.download.path}")
    public void setApplicationDownloadPath(String applicationDownloadPath) {
        ConfigurationConfig.applicationDownloadPath = applicationDownloadPath;
    }

    @Value("${application.pdf.path}")
    public void setApplicationPdfPath(String applicationPdfPath) {
        ConfigurationConfig.applicationPdfPath = applicationPdfPath;
    }

    /**
     * 获取文件上传绝对路径
     * @return
     */
    public static String getDownloadDestDir() {
        return applicationDownloadPath + File.separator + applicationName;
    }

    /**
     * 获取文件上传绝对路径
     * @return
     */
    public static String getUploadDestDir() {
        return applicationUploadPath + File.separator + applicationName;
    }

    /**
     * 获取文件访问host
     * @return
     */
    public static String getVisitHost() {
        return applicationHost + File.separator + applicationName + "/profile";
    }

    /**
     * 根据访问的url获取磁盘的绝对访问路径
     * @return
     */
    public static String getAbsoluteFile(String visitUrl) {
        String[] pathArr = visitUrl.split("profile");
        return getUploadDestDir() + pathArr[1];
    }

    ///**
    // * 用户端小程序支付商户证书 apiclient_key.pem
    // */
    //@Value("${wx.user.wechat.pay.api.client.key}")
    //public String wxUserWechatPayApiClientKey;
    ///**
    // * 用户端小程序商户id
    // */
    //@Value("${wx.user.wechat.merchant.id}")
    //public String wxUserWechatMerchantId;
    ///**
    // * 用户端小程序证书序列号
    // */
    //@Value("${wx.user.wechat.merchant.serial.no}")
    //public String wxUserWechatMerchantSerialNo;
    ///**
    // * 用户端小程序支付 API v3 秘钥
    // */
    //@Value("${wx.user.wechat.apiV3Key}")
    //public String wxUserWechatApiV3Key;

    //@Bean
    //public CloseableHttpClient httpClient() throws IOException {
    //    // 加载商户私钥（privateKey：私钥字符串）
    //    PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(wxUserWechatPayApiClientKey.getBytes("utf-8")));
    //
    //    // 加载平台证书（mchId：商户号,mchSerialNo：商户证书序列号,apiV3Key：V3密钥）
    //    AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(new WechatPay2Credentials(wxUserWechatMerchantId, new PrivateKeySigner(wxUserWechatMerchantSerialNo, merchantPrivateKey)), wxUserWechatApiV3Key.getBytes("utf-8"));
    //
    //    // 初始化httpClient
    //    CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
    //            .withMerchant(wxUserWechatMerchantId, wxUserWechatMerchantSerialNo, merchantPrivateKey)
    //            .withValidator(new WechatPay2Validator(verifier)).build();
    //    return httpClient;
    //}

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        return new ThreadPoolExecutor(20,
                30,
                60L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue(50));
    }
}
